package com.base.app_core.View;

import android.content.Context;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.app_core.R;
import com.base.app_core.util.XYBAUtils;
import com.blankj.utilcode.util.StringUtils;

import java.util.Set;

/**
 * Created by xiangheng.song on 2017/3/18.
 */

public class LinkedLinearLayout extends LinearLayout implements View.OnClickListener {

    private String url;
    private Boolean forceLogin;

    public LinkedLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinkView);
        this.url = a.getString(R.styleable.LinkView_url);
        this.forceLogin = a.getBoolean(R.styleable.LinkView_forceLogin, false);
        a.recycle();
        setOnClickListener(this);
        if(this.forceLogin){
            XYBAUtils.forceLogin(this);
        }
    }

    @Override
    public void onClick(View v) {
        //        Routers.open(getContext(), url);
        if(!StringUtils.isEmpty(url)){
            Uri uri= Uri.parse(url);
            String group=uri.getQueryParameter("group");
            Set<String> paramkeys=uri.getQueryParameterNames();
            Bundle bundle=new Bundle();
            for(String str:paramkeys){
                bundle.putString(str,uri.getQueryParameter(str));
            }
            if(StringUtils.isEmpty(group))
                ARouter.getInstance().build(uri).navigation();
            else
                ARouter.getInstance().build(uri.getPath(),group).with(bundle).navigation();
        }
    }

}
